<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('项目收款') }}</span>
      <span class="sub-header">{{ $t('(仅此查询售后项目收款明细,如未找到项目收款,请更新项目状态)') }}</span>
    </div>
    <el-form v-if="!param" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('项目名称')" prop="dealName">
        <el-input
          v-model="queryParams.dealName"
          :placeholder="$t('项目名称')"
          clearable
          size="small"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('款项名称')" prop="collectionName">
        <el-input
          v-model="queryParams.collectionName"
          :placeholder="$t('款项名称')"
          clearable
          size="small"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('收款时间')">
        <DatePicker v-model="dateRange" type="daterange" />
      </el-form-item>
      <el-form-item :label="$t('收款状态')" prop="collectionState">
        <el-select v-model="queryParams.collectionState" filterable :placeholder="$t('收款状态')" clearable size="small">
          <el-option
            v-for="dict in collectionStateOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealCollectionList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column v-if="!this.$route.query.id" :label="$t('项目名称')" align="center" prop="dealName" width="180" show-overflow-tooltip />
      <el-table-column width="180" :label="$t('款项名称')" align="center" prop="collectionName" />
      <el-table-column :label="$t('关联合同')" align="center" prop="joinContractName" />
      <el-table-column :label="$t('收款时间')" align="center" prop="collectionTime" :sortable="param">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.collectionTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('收款金额') + $t('金额单位')" align="center" prop="collectionMoney" :sortable="param">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.collectionMoney, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('是否开具发票')" align="center" prop="invoice" :formatter="invoiceFormat" width="150" />
      <el-table-column :label="$t('收款状态')" fixed="right" align="center" prop="collectionState" :formatter="collectionStateFormat" width="150" />
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:collection:view']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('查看') }}</el-button>
          <el-button
            v-hasPermi="['deal:collection:view']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="selectHandle(scope.row)"
          >{{ $t('选择') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目收款对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealCollectionForm ref="dealCollectionForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealCollection, findSummaryInfo } from '@/api/deal/collection/dealCollection'
import dealCollectionForm from '@/views/deal/collection/dealCollectionForm'
export default {
  components: {
    dealCollectionForm
  },
  props: {
    param: {
      type: Object,
      default: undefined,
      desc: '查询参数'
    }
  },
  data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 项目收款表格数据
        dealCollectionList: [],
        // 查询 收款时间
        collectionTimes: [],
        // 弹出层标题
        title: '项目收款',
        // 日期范围
        dateRange: [],
        // 是否开具发票字典
        invoiceOptions: [],
        // 收款状态字典
        collectionStateOptions: [],
        formOpen: false,
          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              dealName: undefined,
              collectionName: undefined,
              joinContract: undefined,
              collectionTime: undefined,
              collectionState: undefined,
              startDate: undefined,
              endDate: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
          // 合同下拉
          contractOptions: []
      }
  },
  created() {
    this.getList()
    this.getDicts('invoice_yn').then(response => {
        this.invoiceOptions = response.data
    })
    this.getDicts('collection').then(response => {
        this.collectionStateOptions = response.data
    })
  },
  methods: {
    /** 查询项目收款列表 */
    getList() {
        if (!this.param) {
          this.loading = true
          if (this.collectionTimes != null && this.collectionTimes.length > 1) {
            this.queryParams.startDate = this.collectionTimes[0]
            this.queryParams.endDate = this.collectionTimes[1]
          } else {
            this.queryParams.startDate = undefined
            this.queryParams.endDate = undefined
          }
          listDealCollection(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.dealCollectionList = response.rows
            this.total = response.total
            this.loading = false
          })
        } else {
          this.loading = true
          findSummaryInfo(this.param).then(response => {
            this.dealCollectionList = response.data
            this.loading = false
          })
        }
    },
    // 是否开具发票字典翻译
    invoiceFormat(row) {
      return this.selectDictLabel(this.invoiceOptions, row.invoice)
    },
    // 收款状态字典翻译
    collectionStateFormat(row) {
      return this.selectDictLabel(this.collectionStateOptions, row.collectionState)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: undefined,
            collectionName: undefined,
            joinContract: undefined,
            collectionTime: undefined,
            collectionMoney: undefined,
            invoice: '0',
            collectionState: '0',
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.collectionTimes = []
      this.queryHandle()
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealCollectionForm.init(row)
      })
    },
    /** 选择按钮操作 */
    selectHandle(row) {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.projectCollectionHandle(row)
    }
  }
}
</script>
<style scoped>
.list-table .sub-header{
  font-size: 12px;
  font-family: "Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","微软雅黑",Arial,sans-serif;
  color: #e74c3c !important;
}
</style>
